<template>
  <div class="course-card-list">
    <el-skeleton v-if="loading" :count="4" animated>
      <template #template>
        <div class="skeleton-card">
          <el-skeleton-item variant="image" style="width: 100%; height: 160px" />
          <div style="padding: 14px">
            <el-skeleton-item variant="h3" style="width: 80%" />
            <div style="display: flex; align-items: center; margin-top: 12px">
              <el-skeleton-item variant="text" style="margin-right: 16px; width: 40%" />
              <el-skeleton-item variant="text" style="width: 30%" />
            </div>
          </div>
        </div>
      </template>
    </el-skeleton>

    <div v-else class="course-grid">
      <el-empty v-if="!courses || courses.length === 0" description="暂无课程" />
      <div v-for="course in courses" :key="course.id" class="course-card">
        <router-link :to="`/courses/${course.id}`" class="card-link">
          <div class="card-image">
            <img 
              :src="course.coverImage || defaultCover" 
              :alt="course.title" 
              @error="handleImageError"
            />
            <div class="course-difficulty" :class="difficultyClass(course.difficulty)">
              {{ course.difficulty }}
            </div>
          </div>
          <div class="card-content">
            <h3 class="course-title">{{ course.title }}</h3>
            <p class="course-subtitle">{{ course.subtitle || '暂无介绍' }}</p>
            <div class="course-meta">
              <span class="institution">{{ course.institution }}</span>
              <div class="course-stats">
                <el-tooltip content="浏览次数" placement="top">
                  <span class="stat"><el-icon><View /></el-icon> {{ course.viewCount }}</span>
                </el-tooltip>
                <el-tooltip content="热度" placement="top">
                  <span class="stat"><el-icon><Star /></el-icon> {{ course.heatScore }}</span>
                </el-tooltip>
              </div>
            </div>
          </div>
        </router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { View, Star } from '@element-plus/icons-vue'

// Props
const props = defineProps({
  courses: {
    type: Array,
    default: () => []
  },
  loading: {
    type: Boolean,
    default: false
  }
})

// Default cover image
const defaultCover = ref('/src/assets/default-course.jpg')

// Methods
const handleImageError = (e) => {
  e.target.src = defaultCover.value
}

const difficultyClass = (difficulty) => {
  if (!difficulty) return 'difficulty-unknown'
  
  switch (difficulty) {
    case '初级':
      return 'difficulty-beginner'
    case '中级':
      return 'difficulty-intermediate'
    case '高级':
      return 'difficulty-advanced'
    default:
      return 'difficulty-unknown'
  }
}
</script>

<style lang="scss" scoped>
.course-card-list {
  width: 100%;
}

.skeleton-card {
  width: 100%;
  max-width: 280px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 24px;
}

.course-card {
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.3s, box-shadow 0.3s;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  height: 100%;
  
  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 6px 16px 0 rgba(0, 0, 0, 0.2);
  }
  
  .card-link {
    text-decoration: none;
    color: inherit;
    display: flex;
    flex-direction: column;
    height: 100%;
  }
  
  .card-image {
    position: relative;
    height: 160px;
    overflow: hidden;
    
    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      transition: transform 0.3s;
    }
    
    &:hover img {
      transform: scale(1.05);
    }
    
    .course-difficulty {
      position: absolute;
      top: 10px;
      right: 10px;
      padding: 4px 8px;
      border-radius: 4px;
      font-size: 12px;
      font-weight: 500;
      color: white;
      
      &.difficulty-beginner {
        background-color: #67c23a;
      }
      
      &.difficulty-intermediate {
        background-color: #e6a23c;
      }
      
      &.difficulty-advanced {
        background-color: #f56c6c;
      }
      
      &.difficulty-unknown {
        background-color: #909399;
      }
    }
  }
  
  .card-content {
    padding: 16px;
    flex: 1;
    display: flex;
    flex-direction: column;
    
    .course-title {
      margin: 0 0 8px;
      font-size: 16px;
      font-weight: 600;
      line-height: 1.4;
      color: var(--el-text-color-primary);
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    .course-subtitle {
      font-size: 14px;
      color: var(--el-text-color-secondary);
      margin: 0 0 12px;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    
    .course-meta {
      margin-top: auto;
      display: flex;
      align-items: center;
      justify-content: space-between;
      
      .institution {
        font-size: 12px;
        color: var(--el-text-color-secondary);
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 150px;
      }
      
      .course-stats {
        display: flex;
        gap: 10px;
        
        .stat {
          display: flex;
          align-items: center;
          font-size: 12px;
          color: var(--el-text-color-secondary);
          
          .el-icon {
            margin-right: 4px;
            font-size: 14px;
          }
        }
      }
    }
  }
}
</style> 